full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run '*RunAs "' A_ScriptFullPath '" /restart'
        else
            Run '*RunAs "' A_AhkPath '" /restart "' A_ScriptFullPath '"'
    }
    ExitApp
}

;MsgBox "A_IsAdmin: " A_IsAdmin "`nCommand line: " full_command_line

#HotIf WinExist("ahk_exe Yuanshen.exe")
DllCall("AllocConsole")
Stdout := FileOpen("*", "w")
logger := false
Out(msg) {  
	if (!logger) {
		Return
	} 
	Stdout.Write(msg)
	Stdout.Read(0)
}

Out("hello123`n")

~$Space::
{
	Out("space pressed`n")
}
~$F::
{
	Loop {
		state := GetKeyState("F","P")
		if (state == 0) {
			Out("f 0")
			Return
		}
		if (A_TimeSinceThisHotkey > 400)
		{
			Out("f 1")
			Break
		}
		Sleep 20
	}

	if(state ==0) {

	} else {
		N := 0
		click_n := 0
		x := Random(1296,1448)
		y := Random(784,817)

		Loop
		{
			state := GetKeyState("F","P")
			if (state == 1) { ; f is on
				N -= 1
				if (N <= 0) {
					Send "{F}"
					Out("Sending F")
					tmp := Random(150,250)
					N := Ceil(tmp/20)
				}
				click_n -= 1
				if (click_n <= 0) {
					;MouseClick Left x, y, 1, 5
					Send "{Click " x " " y " Left}"
					Out("click")
					tmp := Random(150,250)
					click_n := Ceil(tmp / 20)
				}
			} else {
				Break
			}
			Sleep 20
		}
	}
	Return
}

$F4::
{
	x := Random(1296,1448)
	y := Random(784,814)

	N :=0
	click_n := 0
	Loop
	{
		color := PixelGetColor(103,37)

		if(color != "0xD8E5EC") {
			Sleep 80
		}
		
		color := PixelGetColor(1759,61)

		if (color == "0xFFFFFF") {
			Break
		}

		N -= 1
		if (N <=0) {
			Send "{F}"
			N := Random(3,7)
		}

		click_n -= 1
		if (click_n <=0) {
			Send "{Click " x " " y " Left}"
			click_n := Random(3,7)
		}

		Sleep 20
	}

	Return
}